package com.bq.gametrade.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bq.gametrade.HttpLink;
import com.bq.gametrade.R;
import com.bq.gametrade.base.BaseActivity;
import com.bq.gametrade.bean.ResponseBean;
import com.bq.gametrade.bean.UserInfoDetails;
import com.bq.gametrade.http.VRHttp;
import com.bq.gametrade.http.VRHttpListener;
import com.bq.gametrade.ui.dialog.CustomerServiceDialog;
import com.bq.gametrade.utils.Base64Util;
import com.bq.gametrade.utils.CheckUtils;
import com.bq.gametrade.utils.IDCardUtils;
import com.bq.gametrade.utils.NetworkUtils;
import com.bq.gametrade.view.BankNamesListSpinner;
import com.bq.gametrade.view.popwindow.BankNameSelectPop;
import com.bq.gametrade.widget.PromptAlertDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BankCardAddActivity extends BaseActivity implements View.OnClickListener{

    private ImageView back,addBankCard;
    private TextView tvTitle;


    private EditText etCardHolder,etBankCardNo,etIdCardNo,etPhoneNo;
    private Button btnConfirm;

    private List<String> list = new ArrayList<>();
    private UserInfoDetails userInfo;
    private TextView tvBankNameSelect;
    private  InputMethodManager imm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bankcard_add);
        if(application.isLogin()){
            userInfo = application.getUserInfo();
        }else {
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        }
        imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        initBankSpinner();
        initView();
    }

    private void initView() {
        back = serchViewById(R.id.image_back);
        back.setOnClickListener(this);
        tvTitle = serchViewById(R.id.title_name);
        tvTitle.setText("添加银行卡");
        etCardHolder = serchViewById(R.id.et_cardholder);
        etBankCardNo = serchViewById(R.id.et_bankCardNo);
        etIdCardNo = serchViewById(R.id.et_idCardNo);
        etPhoneNo = serchViewById(R.id.et_phoneNumber);
        btnConfirm = serchViewById(R.id.btn_confirm);
        btnConfirm.setOnClickListener(this);
        tvBankNameSelect = serchViewById(R.id.tv_bankNameSelect);
        tvBankNameSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(getWindow().getAttributes().softInputMode== WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED){
                    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
                }
                final BankNameSelectPop bankNameSelectPop = new BankNameSelectPop(BankCardAddActivity.this,list);
                bankNameSelectPop.showAsDropDown(tvBankNameSelect);
                bankNameSelectPop.backgroundAlpha(0.6f);
                bankNameSelectPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        bankNameSelectPop.backgroundAlpha(1f);
                    }
                });
                bankNameSelectPop.setSelectBankNameListener(new BankNameSelectPop.OnSelectBankNameListener() {
                    @Override
                    public void selectBankName(int position) {
                        tvBankNameSelect.setText(list.get(position));
                    }
                });

            }
        });
    }

    private void initBankSpinner(){
        list.add("中国工商银行");
        list.add("中国招商银行");
        list.add("中国建设银行");
        list.add("中国农业银行");
        list.add("中国交通银行");
        list.add("中国邮政储蓄银行");
        list.add("中国银行");
        list.add("上海浦东发展银行");
        list.add("广东发展银行");
        list.add("平安银行");
        list.add("华夏银行");
        list.add("中信银行");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image_back:
                finish();
                break;
            case R.id.btn_confirm:
                if(TextUtils.isEmpty(etCardHolder.getText().toString())){
                   toastShow("持卡人姓名未填写");
                    return;
                }
                if(TextUtils.isEmpty(tvBankNameSelect.getText().toString())){
                    toastShow("卡号所属银行您还没有选择");
                    return;
                }
                if(TextUtils.isEmpty(etBankCardNo.getText().toString())){
                    toastShow("银行卡号未填写");
                    return;
                }

                if(etBankCardNo.getText().toString().replace(" ","").length()<16 ){
                    toastShow("银行卡号不正确");
                    return;
                }
                if(etBankCardNo.getText().toString().replace(" ","").length()>19){
                    toastShow("银行卡号不正确");
                    return;
                }


                if(TextUtils.isEmpty(etIdCardNo.getText().toString())){
                    toastShow("持卡人身份证号未填写");
                    return;
                }

//                if(!IDCardUtils.IDCardValidate(etIdCardNo.getText().toString()).equals("")){
//                   toastShow("持卡人身份证号不正确");
//                    return;
//                }
                if(!CheckUtils.isMobile(etPhoneNo.getText().toString())){
                    toastShow("手机号码不正确");
                    return;
                }
                if(!NetworkUtils.isNetworkAvailable(BankCardAddActivity.this)){
                    toastShow("你还没有连接网络哦");
                    return;
                }
                new PromptAlertDialog(BankCardAddActivity.this).builder().setTitle("提示").setTouchOutsideCanceled(true)
                        .setMsg("银行卡绑定之后信息不可修改，可解绑")
                        .setPositiveButton("继续", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                bindBankCardNo();
                            }
                        }).setNegativeButton("取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                }).show();


                break;

        }
    }

    private void bindBankCardNo(){
        Map<String, String> params = new HashMap<>();
        params.put("token",userInfo.token);
        params.put("card_user", Base64.encodeToString(etCardHolder.getText().toString().getBytes(),Base64.DEFAULT));
        params.put("tel",etPhoneNo.getText().toString());
        params.put("bank", Base64.encodeToString(tvBankNameSelect.getText().toString().getBytes(),Base64.DEFAULT));
        params.put("card_num",etBankCardNo.getText().toString().replace(" ",""));
        params.put("id_card",etIdCardNo.getText().toString());
        showSpotsDialog("正在绑定····");
        VRHttp.sendRequest(BankCardAddActivity.this, HttpLink.BIND_BANKCARD_URL, params, new VRHttpListener() {
            @Override
            public void onSuccess(Object response, boolean isCache) {
                closeSpotsDialog();
                if(response != null){
                    ResponseBean bean = (ResponseBean)response;
                    if(bean.status.equals("0")){
                        toastShow("绑定成功·");
                        finish();
                    }else if(bean.status.equals("1")){
                        new PromptAlertDialog(BankCardAddActivity.this).builder().setTitle("提示").setTouchOutsideCanceled(true)
                                .setMsg(Base64Util.decodeStr(bean.message))
                                .setPositiveButton("确定", new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {

                                    }
                                }).setNegativeButton("取消", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                            }
                        }).show();
                    }else if(bean.status.equals("99")){
                        toastShow("登录超时，请重新登录");
                        application.setUserInfo(null);
                        startActivity(new Intent(BankCardAddActivity.this,LoginActivity.class));
                        finish();
                    }else if (bean.status.equals("98")) {
                        toastShow("您的账号已在其它设备登录，请重新登录修改您的密码");
                        application.setUserInfo(null);
                        startActivity(new Intent(BankCardAddActivity.this, LoginActivity.class));
                        finish();
                    }else {
                        toastShow("绑定失败请重试");
                    }
                }

            }

            @Override
            public void onError(String error) {
                super.onError(error);
                closeSpotsDialog();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                closeSpotsDialog();
            }
        });
    }
}
